package priv.tuyou.redis.limit;

import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * 抽象限流器，实现模板代码
 * @author tuyou_cd
 * @date 2021/4/23 15:07
 */
public abstract class AbstractRateLimiter implements RateLimiter {

    String key = "rateLimit:" + UUID.randomUUID().toString();

    /**
     * redis连接池
     */
    JedisPool pool;

    /**
     * 周期（毫秒）
     */
    int period;

    /**
     * 单个周期最大访问次数
     */
    int limit;

    public AbstractRateLimiter(JedisPool pool, int period, int limit) {
        this.pool = pool;
        setLimit(period, limit);
    }

    @Override
    public void setLimit(int period, int limit) {
        this.period = period;
        this.limit = limit;
    }

    @Override
    public boolean visit() {
        return visit(1);
    }
}
